package com.tenbit.tbw.vo;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Map;
 




import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 




import org.springframework.util.FileCopyUtils;
import org.springframework.web.servlet.view.AbstractView;
 
public class PDT1020VO extends AbstractView {
 
 
    public void Download(){
         
        setContentType("application/download; utf-8");
         
    }

	@Override
	protected void renderMergedOutputModel(Map<String, Object> model,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		 File file = (File) model.get("downloadFile");
         
	        response.setContentType(getContentType());
	        response.setContentLength((int)file.length());
	        
	        String fileName = java.net.URLEncoder.encode(file.getName(), "UTF-8");
            response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            
	   /*     if (header.contains("MSIE")) {

	               String docName = URLEncoder.encode(file.getName(),"UTF-8").replaceAll("\\+", "%20");

	               response.setHeader("Content-Disposition", "attachment;filename=" + docName + ";");

	        } else if (header.contains("Firefox")) {

	               String docName = new String(file.getName().getBytes("UTF-8"), "ISO-8859-1");

	               response.setHeader("Content-Disposition", "attachment; filename=\"" + docName + "\"");

	        } else if (header.contains("Opera")) {

	               String docName = new String(file.getName().getBytes("UTF-8"), "ISO-8859-1");

	               response.setHeader("Content-Disposition", "attachment; filename=\"" + docName + "\"");

	        } else if (header.contains("Chrome")) {

	               String docName = new String(file.getName().getBytes("UTF-8"), "ISO-8859-1");

	               response.setHeader("Content-Disposition", "attachment; filename=\"" + docName + "\"");

	        }
*/
	        response.setHeader("Content-Type", "application/octet-stream");
	        response.setHeader("Content-Transfer-Encoding", "binary;");
	        response.setHeader("Pragma", "no-cache;");
	        response.setHeader("Expires", "-1;"); 
	         
	        OutputStream out = response.getOutputStream();
	        FileInputStream fis = null;
	         
	        try {
	            fis = new FileInputStream(file);
	            FileCopyUtils.copy(fis, out);
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            if (fis != null) { try { fis.close(); } catch (Exception e2) {}}
	        }
	        out.flush();
	}
	
/*	private String getBrowser(HttpServletRequest request) {

        String header =request.getHeader("User-Agent");

        if (header.contains("MSIE")) {

               return "MSIE";

        } else if(header.contains("Chrome")) {

               return "Chrome";

        } else if(header.contains("Opera")) {

               return "Opera";

        }

        return "Firefox";

  }
	*/
}
         
